/**
    * Created by haolw on 5/10/17.
    */

bookApp.controller('BookController', function($scope, $rootScope, $http, $filter,ngTableParams,$resource, $modal,BookTypeTree) {

    $scope.paginationConf={
        currentPage:1,
        totalItems:0,
        itemsPerPage: 30
    };

    $scope.bookTableParams = new ngTableParams({
        page: 1,
        count: 100
    },{
        total: 0,
        counts:[],
        getData: function ($defer, params) {
            var queryParams = {pageNo:$scope.paginationConf.currentPage,pageSize:$scope.paginationConf.itemsPerPage};
            $http.get("/api/book",{params:queryParams}).success(function(data){
                if(data.code == 200){
                    $scope.books = data.data.result;
                    $scope.paginationConf.totalItems = data.data.totalCount;
                    $defer.resolve($scope.books);
                }
            });
        }
    });

    $scope.$watch('paginationConf.currentPage + paginationConf.itemsPerPage', function () {
        $scope.bookTableParams.reload();
    });


    $scope.openAddBookModal = function(){
        var modalInstance = $modal.open({
            templateUrl : 'bookModal.html',
            controller : 'bookModalCtrl',
            size : 'md',
            resolve : {
                book : function(){
                    return {};
                }
            }
        });
        modalInstance.result.then(function (book) {
            $scope.bookTableParams.reload();
        },function () {

        });
    }


    $scope.openEditBookModal = function (code) {
        var modalInstance = $modal.open({
            templateUrl : 'bookModal.html',
            controller : 'bookModalCtrl',
            size : 'md',
            resolve : {
                book : function(){
                    return {code:code};
                }
            }
        });
        modalInstance.result.then(function (book) {
            $scope.bookTableParams.reload();
        },function () {

        });
    }

    $scope.openDelBooksModal = function (book) {
        var modalInstance = $modal.open({
            templateUrl : 'delBookModal.html',
            controller : 'delBookModalCtrl',
            size : 'md',
            resolve : {
                book : function(){
                    return book;
                }
            }
        });
        modalInstance.result.then(function () {
            $scope.bookTableParams.reload();
        },function () {

        });
    }

    $scope.openBorrowBookModal = function (book) {
        var modalInstance = $modal.open({
            templateUrl : 'borrowBookModal.html',
            controller : 'borrowBookModalCtrl',
            size : 'md',
            resolve : {
                book : function(){
                    return book;
                }
            }
        });
        modalInstance.result.then(function () {
            $scope.bookTableParams.reload();
        },function () {

        });
    }

    $scope.openReturnBookModal = function (book) {
        var modalInstance = $modal.open({
            templateUrl : 'returnBookModal.html',
            controller : 'returnBookModalCtrl',
            size : 'md',
            resolve : {
                book : function(){
                    return book;
                }
            }
        });
        modalInstance.result.then(function () {
            $scope.bookTableParams.reload();
        },function () {

        });
    }
});

bookApp.controller('bookModalCtrl', function($scope, $rootScope, $http, $filter,$resource, $modalInstance,book,BookTypeTree) {
    $scope.book = angular.copy(book);
    if(!book.code)
        $scope.book.type = 1;
    else{
        $http.get("/api/book/"+$scope.book.code).success(function (data) {
            $scope.book = data.data;
        })
    }
    BookTypeTree.load($scope);

    $scope.ready = function () {
        if($scope.book.code){

        }
    }
    $scope.selectNode = function(e, data) {
        if(data.node.id != 'root')
           $scope.book.classification = data.node.id;
    };

    $scope.save = function(){
        if($scope.book.id){
            $http.put("/api/book",$scope.book).success(function (data) {
                if(data.status == 400){
                    $scope.errorMsg = "输入信息消息，请重新";
                    return;
                }
                $modalInstance.close($scope.book);
            }).error(function (data) {
                if(data.status == 400){
                    $scope.errorMsg = "输入信息消息，请重新";
                }
            });
        }else{
            $http.post("/api/book",$scope.book).success(function (data) {
                console.info(data);
                if(data.status == 400){
                    $scope.errorMsg = "输入信息消息，请重新";
                    return;
                }
                $modalInstance.close($scope.book);
            }).error(function (data) {
                if(data.status == 400){
                    $scope.errorMsg = "输入信息消息，请重新";
                }
            });
        }


    }
    $scope.cancel = function(){
        $modalInstance.dismiss();
    }

});

bookApp.controller('delBookModalCtrl', function($scope, $rootScope, $http,$modalInstance,book) {

    $scope.ok = function () {
        $http.delete("/api/book/"+book.code).success(function () {
            $modalInstance.close();
        });
    }

    $scope.cancel = function () {
        $modalInstance.dismiss();
    }
});
bookApp.controller('borrowBookModalCtrl', function($scope, $rootScope, $http,$modalInstance,book) {
    $scope.ok = function () {
        $http.put("/api/book/borrow",book).success(function () {
            $modalInstance.close();
        }).error(function (data) {
            $scope.errorMsg = "无法借阅：图书已有人借阅！";
        });
    }

    $scope.cancel = function () {
        $modalInstance.dismiss();
    }
});
bookApp.controller('returnBookModalCtrl', function($scope, $rootScope, $http,$modalInstance,book) {
    $scope.ok = function () {
        $http.put("/api/book/return-book",book).success(function () {
            $modalInstance.close();
        }).error(function (data) {
            $scope.errorMsg = "归还图书出错请稍后重试！";
        });
    }

    $scope.cancel = function () {
        $modalInstance.dismiss();
    }
});